﻿using System;
using System.Collections.Generic;
using Runtime.DesignPattern;

namespace Plugins.FormicArch.Core
{

    public class FAEventBaseManager<TKey>:GameSingleton< FAEventBaseManager<TKey>>
    {
          // 存储事件类型和对应的处理器列表
        private readonly Dictionary<TKey, List<object>> _eventHandlers =
            new Dictionary<TKey, List<object>>();
        
        // 触发事件处理器的通用方法
        public void TriggerHandler(TKey eventType)
        {
            if (_eventHandlers.TryGetValue(eventType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    if (handler is Action action)
                    {
                        action();
                    }
                }
            }
        }

        // 触发事件处理器的通用方法
        public void TriggerHandler<T>(TKey eventType, T arg)
        {
            if (_eventHandlers.TryGetValue(eventType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    if (handler is Action<T> action)
                    {
                        action(arg);
                    }
                }
            }
        }

        public void TriggerHandler<T1, T2>(TKey eventType, T1 arg1, T2 arg2)
        {
            if (_eventHandlers.TryGetValue(eventType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    if (handler is Action<T1, T2> action)
                    {
                        action(arg1, arg2);
                    }
                }
            }
        }

        public void TriggerHandler<T1, T2, T3>(TKey eventType, T1 arg1, T2 arg2, T3 arg3)
        {
            if (_eventHandlers.TryGetValue(eventType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    if (handler is Action<T1, T2, T3> action)
                    {
                        action(arg1, arg2, arg3);
                    }
                }
            }
        }

        public void TriggerHandler<T1, T2, T3, T4>(TKey eventType, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
        {
            if (_eventHandlers.TryGetValue(eventType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    if (handler is Action<T1, T2, T3, T4> action)
                    {
                        action(arg1, arg2, arg3, arg4);
                    }
                }
            }
        }

        public void TriggerHandler<T1, T2, T3, T4, T5>(TKey eventType, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
            T5 arg5)
        {
            if (_eventHandlers.TryGetValue(eventType, out var handlers))
            {
                foreach (var handler in handlers)
                {
                    if (handler is Action<T1, T2, T3, T4, T5> action)
                    {
                        action(arg1, arg2, arg3, arg4, arg5);
                    }
                }
            }
        }


        // 注册事件处理器的通用方法
        public void RegisterHandler(TKey eventType, Delegate handler)
        {
            if (handler == null)
                throw new ArgumentNullException(nameof(handler));


            if (!_eventHandlers.ContainsKey(eventType))
            {
                _eventHandlers[eventType] = new List<object>();
            }

            _eventHandlers[eventType].Add(handler);
        }

        // 取消注册事件处理器的通用方法
        public void UnregisterHandler(TKey eventType, Delegate handler)
        {
            if (handler == null)
                throw new ArgumentNullException(nameof(handler));


            if (_eventHandlers.ContainsKey(eventType))
            {
                _eventHandlers[eventType].Remove(handler);

                // 如果事件类型没有处理器，则移除事件类型
                if (_eventHandlers[eventType].Count == 0)
                {
                    _eventHandlers.Remove(eventType);
                }
            }
        }
        
        // 注册事件处理器的通用方法
        public void RegisterHandler(TKey eventType, object handler)
        {
            if (handler == null)
                throw new ArgumentNullException(nameof(handler));


            if (!_eventHandlers.ContainsKey(eventType))
            {
                _eventHandlers[eventType] = new List<object>();
            }

            _eventHandlers[eventType].Add(handler);
        }

        // 取消注册事件处理器的通用方法
        public void UnregisterHandler(TKey eventType, object handler)
        {
            if (handler == null)
                throw new ArgumentNullException(nameof(handler));


            if (_eventHandlers.ContainsKey(eventType))
            {
                _eventHandlers[eventType].Remove(handler);

                // 如果事件类型没有处理器，则移除事件类型
                if (_eventHandlers[eventType].Count == 0)
                {
                    _eventHandlers.Remove(eventType);
                }
            }
        }
    }
}